package com.yanqu.road.server.cmd.coupon;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.coupon.CouponActivityUserData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.MallInfo;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.recharge.CouponProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.coupon.CouponActivityModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.config.MallMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.List;

@Cmd(code = Protocol.S_COUPON_ITEM_TRIGGER, desc = "代金券触发")
public class CouponTriggerCmd extends AbstractCommand {
	private int activityId;
	private int mallId;
	private int type;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CouponProto.CouponItemTriggerReqMsg reqMsg = CouponProto.CouponItemTriggerReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		mallId = reqMsg.getMallId();
		type = reqMsg.getType();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
		if(null == activityInfo){
//			ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
			return;
		}
		if(!ActivityHelper.activityInTime(activityInfo)){
//			ret = GameErrorCode.E_ACTIVITY_OVER_TIME;
			return;
		}
		MallInfo mallInfo = MallMgr.getMallInfoByMallId(mallId);
		if(null == mallInfo){
//			ret = GameErrorCode.E_MALL_NO_EXIST;
			return;
		}
		List<GoodsInfo> goodsInfoList = GoodsMgr.getCouponGoodsList(mallInfo);
		if(goodsInfoList.size() <= 0){
//			ret = GameErrorCode.E_COUPON_ITEM_MALL_CANNOT_TRIGGER;
			return;
		}
		CouponActivityUserData couponActivityUserData = player.getModule(CouponActivityModule.class).getCouponActivityUserData();
		if(null == couponActivityUserData || couponActivityUserData.getActivityId() != activityId){
			return;
		}
		if(couponActivityUserData.getCouponTriggerTimes(type) > 1){
			return;
		}
		couponActivityUserData.addCouponTriggerTimes(type, 1);
		GoodsInfo couponGoods = goodsInfoList.get(0);
		Property reward = new Property(couponGoods.getGoodsId(), BigInteger.ONE);
		player.getModule(CurrencyModule.class).addCurrency(reward, eLogMoneyType.Activity, eLogMoneyType.CouponActivityTriggerReward);
	}

	@Override
	public void sendData(GamePlayer player) {
		CouponProto.CouponItemTriggerRespMsg.Builder respMsg = CouponProto.CouponItemTriggerRespMsg.newBuilder();
		respMsg.setRet(ret);
		player.sendPacket(Protocol.U_COUPON_ITEM_TRIGGER, respMsg);
	}
}
